package com.sinkdream.cms.service.serviceimpl;

import com.sinkdream.cms.dao.NewsMapper;
import com.sinkdream.cms.model.News;
import com.sinkdream.cms.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;
    @Override
    public void publishNews(News news) {
        if (news.getNewsClicked() == null ||news.getNewsClicked().equals("")){
            news.setNewsClicked(0);
        }
        /*
        * 这里需要对用户登录进行判断，拿到用户登录的token之后通过用户权限对新闻类型进行处理
        **/
        if(news.getNewsType()==null || news.getNewsType().equals("")){
            // TO-DO
            news.setNewsType(true);
        }
        /* 这里要拿登录的用户名*/
        if(news.getNewsAuthor()==null || news.getNewsAuthor().equals("")){
            //todo
            news.setNewsAuthor("100001");
        }
        if(news.getNewsZan() == null || news.getNewsZan().equals("")){
            news.setNewsZan(0);
        }
       newsMapper.insert(news);
    }

    @Override
    public List<News> getNewsList() {
        List<News> newsList = newsMapper.getNewsListByManager();
        return newsList;
    }

    @Override
    public News getNewsById(int newsId) {
        News record = newsMapper.getNewsById(newsId);
        if(record == null){
            //进行异常处理
            return null;
        }else{
            return record;
        }
    }

    @Override
    public void updateNews(News news) {

    }

    @Override
    public void deleteById(int id) {
        newsMapper.deleteNewsById(id);
    }

}
